<?php
/**
 *（Logic）
 */
namespace App\Logic\Amazon\Api;

use App\Models\Amazon\AmazonReportCondition;
use App\Models\Basics\AmazonAccount;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use SellingPartnerApi\ReportType;

class ReportWork
{

    /**
     * 创建报告(测试，非正式)
     * @param null $date
     * @param null $reportType
     * @param int $accountId
     * @throws \Throwable
     */
    /*public static function createReportData($date = null, $reportType = null, $accountId = 0){

        ini_set('memory_limit', '200M');
        set_time_limit(0);

        $eof = $date;
        $accounts = AmazonAccount::_getAccounts($accountId);
        foreach ($accounts as $account){
            $requiredConfig = RequiredConfig::getInit();
            $config = $requiredConfig->configuration($account->refresh_token,$account->aws_region);
            $marketplace_ids = $account->account_country->pluck('marketplace_id')->toArray();

            $time = strtotime($date);
            while (true){
                if($time > strtotime($eof)) break;

                $paramDate = date('Y-m-d',$time);
                echo '------start:'.$paramDate.'-'.$account->account.'（'.$account->id.'）------'.PHP_EOL;
                $data = [
                    'report_options' => [
                        'dateGranularity' => 'DAY',
                        'asinGranularity' => 'CHILD'
                    ],
                    'data_start_time' => $paramDate,
                    'data_end_time' => $paramDate,
                    'report_type' => $reportType
                ];
                Report::apiCreateReport($config,$account,$data,$marketplace_ids,true,1);

                $time += 86400;
                sleep(5);
            }
            sleep(10);
        }
        dd('success!');
    }*/

    /**
     * 获取并解析报告(测试，非正式)
     * @param null $date
     * @param null $reportType
     * @param int $accountId
     * @throws \Throwable
     */
    /*public static function getAndParseReportData($date = null, $reportType = null, $accountId = 0){

        ini_set('memory_limit', '200M');
        set_time_limit(0);

        $accounts = AmazonAccount::_getAccounts($accountId);
        $eof = $date;
        foreach ($accounts as $account){
            $requiredConfig = RequiredConfig::getInit();
            $apiConfig = $requiredConfig->configuration($account->refresh_token,$account->aws_region);

            $time = strtotime($date);
            while (true){
                if($time > strtotime($eof)) break;
                echo '------start:'.$date.'-'.$account->account.'（'.$account->id.'）------'.PHP_EOL;
                Report::apiGetAndParseReport($apiConfig,$account,$reportType,$date);

                $time += 86400;
                sleep(5);
            }
            sleep(10);
        }
        dd('success!');
    }*/

    /**
     * 亚马逊结算报告
     * @param $report_types
     * @param $created_since
     * @param $created_until
     * @param array $where
     * @return void
     */
    /*public static function settlementReport($report_types,$created_since,$created_until, array $where = []): void
    {
        $where['api'] = 1;
        $accounts = AmazonAccount::optionsForApi($where);
        $re = [];
        foreach ($accounts as $account){
            $marketplace_ids = $account->account_country->pluck('marketplace_id')->toArray();
            $requiredConfig = RequiredConfig::getInit();
            $apiConfig = $requiredConfig->configuration($account->refresh_token,$account->aws_region);
            $re[$account->id] = Report::apiGetReports($apiConfig,$account,$report_types,['DONE'],$marketplace_ids,10,$created_since,$created_until);
        }
        var_dump($re);
    }*/


    /**
     * 获取报告(站点)
     * @param $paramData
     * @param array $accountIds
     * @throws \Throwable
     */
    public static function getReportData($paramData, $accountIds = []){
        ini_set('memory_limit', '200M');
        set_time_limit(0);

        $accounts = AmazonAccount::query()->whereIn('id', $accountIds)->get();
        $re = [];
        foreach ($accounts as $account){
            $requiredConfig = RequiredConfig::getInit();
            $apiConfig = $requiredConfig->configuration($account->refresh_token,$account->aws_region);
            $marketplace_ids = $account->account_country->pluck('marketplace_id')->toArray();
            if (!$marketplace_ids) continue;
            $re[$account->id] = Report::apiCreateReport($apiConfig,$account,$paramData,$marketplace_ids,true,1);
            sleep(2);
        }
        var_dump($re);
    }

    /**
     * 获取并解析报告(站点)
     * @param $reportType
     * @param array $accountIds
     * @throws \Throwable
     */
    public static function getAndParseReportData($reportType,$accountIds = []){
        ini_set('memory_limit', '200M');
        set_time_limit(0);

        $accounts = AmazonAccount::query()->whereIn('id', $accountIds)->get();
        foreach ($accounts as $account){
            $requiredConfig = RequiredConfig::getInit();
            $apiConfig = $requiredConfig->configuration($account->refresh_token,$account->aws_region);
            Report::apiGetAndParseReport($apiConfig,$account,$reportType);
            sleep(2);
        }
        dd('success!');
    }


    /**
     * 创建订单报告
     * @param $paramData
     * @param $multi
     * @throws \Throwable
     */
    /*public static function createOrdersReportData($paramData, $multi){

        ini_set('memory_limit', '200M');
        set_time_limit(0);

        $accounts = AmazonAccount::query()->whereIn('id', [2])->get();
        $re = [];
        foreach ($accounts as $account){
            $requiredConfig = RequiredConfig::getInit();
            $apiConfig = $requiredConfig->configuration($account->refresh_token,$account->aws_region);
            $marketplace_ids = $account->account_country->pluck('marketplace_id')->toArray();
            if (!$marketplace_ids) continue;
            $re[$account->id] = Report::apiCreateReport($apiConfig,$account,$paramData,$marketplace_ids,$multi,1);
            sleep(2);
        }
        var_dump($re);
    }*/

    /**
     * 获取订单报告
     * @param $paramData
     * @throws \Throwable
     */
    /*public static function getAndParseOrdersReportData($reportType){

        ini_set('memory_limit', '200M');
        set_time_limit(0);

        $accounts = AmazonAccount::query()->whereIn('id', [2])->get();
        $re = [];
        foreach ($accounts as $account){
            $requiredConfig = RequiredConfig::getInit();
            $apiConfig = $requiredConfig->configuration($account->refresh_token,$account->aws_region);
            $re[$account->id] = Report::apiGetAndParseReport($apiConfig,$account,$reportType);
            sleep(2);
        }
        var_dump($re);
    }*/

}
